<template>
  <div>
    <el-row>
      <el-col :span="24"><div class="grid-content bg-purple-light">
        <div style="float: left; margin: 20px 100px; font-weight: 700;color:#606266;font-size: 20px"><i class="el-icon-search"></i>角色查询</div>
      </div></el-col>
    </el-row>
    <!--  查询条件-->
    <el-form :inline="true" :model="queryForm" style="text-align: left; margin: 0 10%">
      <el-form-item label="角色名称">
        <el-input v-model="queryForm.name" placeholder="角色名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="getList" plain>查询</el-button>
        <el-button type="info" @click="add()" plain>新增</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="24"><div class="grid-content bg-purple-light">
        <div style="float: left; margin: 0 100px; font-weight: 700;color:#606266;font-size: 20px"><i class="el-icon-s-unfold"></i>数据列表</div>
      </div></el-col>
    </el-row>
    <div id="s" class="animate__animated animate__zoomIn animate__delay-1s" style="margin-top: 20px">
    <!--  数据表格-->
    <el-table :data="tableData" stripe style="width: 100%;" :border="true"
              :header-cell-style="{background:'#209e91', color:'#ffffff',textAlign:'center'}">
      <el-table-column
        prop="name"
        label="角色名称"
        align="center">
      </el-table-column>
      <el-table-column
        prop="mark"
        label="角色备注"
        align="center">
      </el-table-column>
      <el-table-column
        prop="createId"
        label="新增人员"
        align="center">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="新增时间"
        align="center">
      </el-table-column>
      <el-table-column
        prop="modifyId"
        label="修改人员"
        align="center">
      </el-table-column>
      <el-table-column
        prop="modifyTime"
        label="修改时间"
        align="center">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center">
        <template v-slot="scope">
          <el-button @click="edit(scope.row)" type="text" size="small" >编辑</el-button>
          <el-popconfirm title="确定删除吗？" @confirm="remove(scope.row)">
            <el-button slot="reference" type="text" size="small" >删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
      <el-row>
        <el-col :span="24"><div class="grid-content bg-purple-light">
          <div style="margin: 20px"></div>
        </div></el-col>
      </el-row>
    <!--  分页-->
    <el-pagination
      @size-change="getList"
      @current-change="getList"
      :current-page.sync="queryForm.pageNum"
      :page-sizes="[10, 20, 30, 40, 60, 80, 100]"
      :page-size.sync="queryForm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
      <el-row>
        <el-col :span="24"><div class="grid-content bg-purple-light">
          <div style="margin: 20px"></div>
        </div></el-col>
      </el-row>
    </div>
    <!--  新增/修改页面-->
    <el-dialog
      :title="title"
      :visible.sync="show"
      width="20%">
      <span>
        <el-form :model="form" :rules="rules" ref="ruleForm" label-width="80px">
          <el-form-item label="角色名称" prop="name">
             <el-input v-model="form.name"></el-input>
          </el-form-item>
           <el-form-item label="角色备注" prop="mark">
             <el-input v-model="form.mark"></el-input>
          </el-form-item>
          <el-form-item label="拥有权限">
            <el-tree
              :data="sysAuthTreeData"
              show-checkbox
              node-key="id"
              ref="tree"
              highlight-current
              :props="{children:'children',label:'name'}">
            </el-tree>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "SysRole",
  data() {
    return {
      //查询对象
      queryForm: {
        name: '',
        pageNum: 1,
        pageSize: 10
      },
      //表格数组
      tableData: [],
      //权限树形菜单
      sysAuthTreeData: [],
      //共多少条
      total: 0,
      //表单对象
      form: {},
      //显示新增、修改
      show: false,
      //标题
      title: '',
      //校验对象
      rules: {
        name: [
          {required: true, message: '请输入用角色名称', trigger: 'blur'},
          {min: 1, max: 40, message: '长度在 1 到 40 个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    getList() {
      this.$axios({
        url: 'sys/role/list',
        method: 'post',
        data: this.queryForm
      }).then(res => {
        if(res.data.success){
          this.tableData = res.data.object.list;
          this.total = res.data.object.total;
        }
      }).catch(err => {
        this.$message.error(err.message);
      })
    },
    getSysAuthTreeData() {
      this.$axios({
        url: 'sys/auth/list',
        method: 'post'
      }).then(res => {
        if(res.data.success){
          this.sysAuthTreeData = res.data.object;
        }
      }).catch(err => {
        this.$message.error(err.message);
      })
    },
    success() {
      this.getList()
    },
    add() {
      //设置标题
      this.title = '新增'
      //显示对话框
      this.show = true
      //清空表单
      this.form = {}
      //清空校验结果
      this.$nextTick(() => {
        this.$refs.tree.setCheckedKeys([]);
        this.$refs['ruleForm'].clearValidate();
      })
    },
    edit(row) {
      console.info(row)
      this.title = '修改'
      //清空表单
      this.form = {}
      //给form对象赋值
      this.form = Object.assign({}, row);
      //清空密码
      this.form.password = ''
      //显示对话框
      this.show = true
      //清空校验结果
      this.$nextTick(() => {
        this.$refs.tree.setCheckedKeys([]);
        console.info(row.sysAuthList);
        if(row.sysAuthList) {
          let sysAuths=[];
          for(let i=0;i<row.sysAuthList.length;i++){
            if(row.sysAuthList[i].menuIndex.indexOf('-')!=-1){
              sysAuths.push(row.sysAuthList[i]);
            }
          }
          this.$refs.tree.setCheckedNodes(sysAuths);
        }
        this.$refs['ruleForm'].clearValidate();
      })
    },
    remove(row) {
      this.$axios({
        url: 'sys/role/remove/' + row.id,
        method: 'post'
      }).then(res => {
        if (res.data.success) {
          this.$message({
            message: res.data.message,
            type: 'success'
          });
          this.getList();
        } else {
          this.$message.error(res.data.message);
        }
      }).catch(err => {
        this.$message.error(err.message);
      })
    },
    submit() {
      this.form.sysAuthList = this.$refs.tree.getCheckedNodes(false,true);
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          //校验通过
          if (this.title == '新增') {
            this.$refs['ruleForm'].validate((valid) => {
              if (valid) {
                this.$axios({
                  url: 'sys/role/add',
                  method: 'post',
                  data: this.form
                }).then(res => {
                  if (res.data.success) {
                    this.show = false;
                    this.getList()
                  } else {
                    this.$message.error(res.data.message);
                  }
                }).catch(err => {
                  this.$message.error(err.message);
                })
              }
            });
          } else {
            this.$axios({
              url: 'sys/role/edit',
              method: 'post',
              data: this.form
            }).then(res => {
              if (res.data.success) {
                this.show = false;
                this.getList()
              } else {
                this.$message.error(res.data.message);
              }
            }).catch(err => {
              this.$message.error(err.message);
            })
          }
        }
      });
    }
  },
  created() {
    this.getList()
    this.getSysAuthTreeData()
  }
}
</script>

<style scoped>
.el-select {
  width: 100%;
}
.el-button--info:hover {
  background: #209e91;
  border-color: #209e91;
  color: #FFF;
}
#s{
  width:90%;
  margin:10px auto;
  background-color: #ffffff;
  box-shadow: 10px 10px 5px #c4c6c7;
  border-radius: 5px
}
</style>
